package com.health.webservices;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import com.health.model.DZJMJKKXcrzBean;
import com.health.tools.GlobalConts;
import com.health.tools.OnWebServiceLinstener;
import com.health.webservices.dzjmjkkxml.DZJMJKKXcrzParser;
import com.health.webservices.dzjmjkkxml.PullDZJMJKKXcrzParser;
import com.novabluetooth.services.BluetoothConstant;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

/**
 * 居民健康电子卡
 * 
 * @author nova
 *
 */
public class JmdzjkkWebService implements OnWebServiceLinstener {
	private static final String TAG = "JmdzjkkWebService";
	private static final String NAMESPACE = WebServiceConts.WEBSERVICE_URL_ZC_DZJMJKK_NAMESPACE;
	private static final String METHOD_NAME = WebServiceConts.WEBSERVICE_URL_ZC_DZJMJKK_METHODNAME;
	private static final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
	private SoapObject mRequest;
	private SoapSerializationEnvelope mEnvelope;
	@SuppressLint("SimpleDateFormat")
	private SimpleDateFormat mSdf = new SimpleDateFormat("yyyy-MM-dd");
	private OnWebServiceLinstener mLinstener;
	public int mSyncCount = 1;
	public int mDataSize = 0;
	private String arg0 = "";// 参数0
	private String arg1 = "";// 参数1
//	private String type = "";// 类型
	private Context mContext;

	public void setOnWebServiceLinstener(OnWebServiceLinstener linstener) {
		this.mLinstener = linstener;
	}

	public JmdzjkkWebService(Context context) {
		this.mContext = context;
	}

	public void synchrodata(String arg0, String arg1 ) throws Exception {
		this.arg0 = arg0;
		this.arg1 = arg1;
//		this.type = type;
		Log.e(TAG, arg0 + "\n" + arg1);
		new Thread(networkTask).start();

	}

	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			Bundle data = msg.getData();
			String val = data.getString("value");
			Log.i("mylog", "请求结果为-->" + val);
			// TODO
			// UI界面的更新等相关操作
		}
	};
	/**
	 * 网络操作相关的子线程
	 */
	Runnable networkTask = new Runnable() {

		@Override
		public void run() {
			try {
				mSyncCount = 1;
				mDataSize = 0;
				 
				mRequest = new SoapObject(NAMESPACE, METHOD_NAME);
				mEnvelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER10);

				mRequest.addProperty("arg0", arg0);

				mRequest.addProperty("arg1", arg1);

				mEnvelope.bodyOut = mRequest;
				mEnvelope.dotNet = false;

				HttpTransportSE httpTransportSE = new HttpTransportSE(
						GlobalConts.getUrlJMDZJKK() + WebServiceConts.WEBSERVICE_URL_ZC_DZJMJKK);
				Log.e("Sebservice:",
						"获取健康卡地址" + GlobalConts.getUrlJMDZJKK() + WebServiceConts.WEBSERVICE_URL_ZC_DZJMJKK);

				httpTransportSE.call(SOAP_ACTION, mEnvelope);

				start("正在建立电子居民健康卡...");
				try {
					SoapPrimitive response = (SoapPrimitive) mEnvelope.getResponse();// 获得返回对象
					// SoapObject response = (SoapObject)
					// mEnvelope.bodyIn ;// 获得返回对象
					// Object response = ( Object)
					// mEnvelope.bodyIn;// 获得返回对象
					Log.e("顶顶顶顶---", "数据：" + response);
					if (response != null) {
						String strres = response.toString();

						complete(strres);
					}
				} catch (Exception e) {
					e.printStackTrace();
					complete("数据同步失败");
				}

			} catch (IOException | XmlPullParserException e) {
				e.printStackTrace();
				onError("IOException | XmlPullParserException" + e.getMessage());
				complete("数据同步失败");
			} catch (Exception e) {
				e.printStackTrace();
				onError("Exception" + e.getMessage());
				complete("数据同步失败");
			}
		}
	};

	@Override
	public void start(String message) {
		if (mLinstener != null) {
			mLinstener.start(message);
		}
	}

	@Override
	public void onSynchronize(String message, int syncCount, int dataSize) {
		if (mLinstener != null) {
			mLinstener.onSynchronize(message, syncCount, mDataSize);
		}
	}

	@Override
	public void complete(String message) {
		if (mLinstener != null) {
			mLinstener.complete(message);
		}
	}

	@Override
	public void onError(String message) {
		if (mLinstener != null) {
			mLinstener.onError(message);
		}
	}

	@Override
	public void onUpload() {
		// TODO Auto-generated method stub

	}

}
